//
//  MainViewController.swift
//  武汉微博
//
//  Created by 谌桃 on 16/9/20.
//  Copyright © 2016年 ryan.chen. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //viewDidLoad中  没有tabBar的tabBarItem ， 但是有子控件
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // TODO : 测试网络
        NetworkTools.sharedManager.request(.POST, URLString: "https://httpbin.org/post", parameters: ["xiaohua":"name"]) { (result) -> Void in
            
            if result == nil {
            
                print("网络不给力")
                return
            }
        }

        addChildViewControllers()
        
        setUPTabBar()
        
        //设置tabBar 的 tintColor
//        tabBar.tintColor = UIColor.orangeColor()
    }
    
    // MARK: - 编辑按钮的点击事件 ,消息传递，使用 @objc 将 OC的方法和swift类关联起来，
    @objc private func composeButtonClick(){
        
        print("dayin")
    }
    
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
        //将按钮设置到顶部
        tabBar.bringSubviewToFront(composeButton)
        
        //设置按钮的frame, -1是为了容错处理
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        // > 0  向内缩进
        // < 0  向外
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
        
        print(tabBar.subviews)
    }
    
    // MARK: - 私有控件
    private lazy var composeButton:UIButton = UIButton(
        titleName: "",
        titleColor: UIColor.darkGrayColor(),
        imageName:"tabbar_compose_icon_add" ,
        backImageName: "tabbar_compose_button")
}




// MARK: - 设置tabBar
extension MainViewController{

    private func setUPTabBar(){
    
        //设置tabBar的背景图片
        //如果提供图片，图片会自动拉伸
        tabBar.shadowImage = UIImage.st_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        tabBar.addSubview(composeButton)
    }
}

// MARK: - 添加子控制器
//通过extension 将相关代码进行分组放置
extension MainViewController{
    
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName:"tabbar_home" )
        addChildViewController(MessageViewController(), title: "消息", imageName:"tabbar_message_center" )
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName:"tabbar_discover" )
        addChildViewController(ProfileViewController(), title: "我的", imageName:"tabbar_profile" )
    }
    
    //addChildViewController(childController: UIViewController) 跟系统的名字相同，参数不同，重载
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
      
        //homeVC.title 从内向外设置，从当前控制器到nav，到tabBar
        vc.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState:.Selected)
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中时的图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let homeNav = UINavigationController(rootViewController: vc)
        
        addChildViewController(homeNav)
    }
}
